﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CapstoneProjectAEReader.Entities.Models;
using iTextSharp.text.pdf;
using CapstoneProjectAEReader.Common.Utility;
using eBdb.EpubReader;
using System.Threading;

namespace CapstoneProjectAEReader.Business.Others
{
    public static class EbookObjectGenerator
    {
        public static Ebook GenerateEbookObj(string filePath)
        {
            Ebook myEbook = new Ebook();
            //File type
            FileInfo temp = new FileInfo(filePath);
            if (temp.Extension.Equals(Constants.EbookType.Pdf, StringComparison.InvariantCultureIgnoreCase))
            {
                myEbook.Type = Constants.EbookType.Pdf;
            }
            if (temp.Extension.Equals(Constants.EbookType.Epub, StringComparison.InvariantCultureIgnoreCase))
            {
                myEbook.Type = Constants.EbookType.Epub;
            }
            //Create Ebook MetaData
            if (Constants.EbookType.Pdf.Equals(myEbook.Type))
            {
                PdfReader metaReader = new PdfReader(filePath);
                myEbook.PageCount = metaReader.NumberOfPages;
                myEbook.Title = Path.GetFileNameWithoutExtension(filePath);
                if (!metaReader.Info.ContainsKey(Constants.PdfInfo.Author))
                {
                    myEbook.Author = Constants.CommonCharacters.Empty;
                }
                else
                {
                    myEbook.Author = metaReader.Info[Constants.PdfInfo.Author].Trim();
                    if (String.IsNullOrWhiteSpace(myEbook.Author))
                    {
                        myEbook.Author = Constants.Na;
                    }
                }
                myEbook.Path = filePath;
                myEbook.ImportDate = DateTime.Now.ToString(Constants.FullDateTimeFormatToSecond);
                myEbook.Rating = 0;
                myEbook.Publisher = Constants.CommonCharacters.Empty;
                myEbook.CurrentPage = 1;
                metaReader.Close();

                myEbook.Group = new List<string>();
                myEbook.BookmarkPath = Constants.StorageStructure.TempFolder + Constants.CommonCharacters.SlashString +
                                       myEbook.Title + Constants.XmlExtension;
                //TODO later
            }
            else if (Constants.EbookType.Epub.Equals(myEbook.Type))
            {
                Epub getEpubBook = new Epub(filePath);
                string activePageCount = getEpubBook.GetContentAsHtml();
                myEbook.Title = "Unknown";
                if (getEpubBook.Title[0] != null)
                {
                    myEbook.Title = getEpubBook.Title[0];
                }
                if (getEpubBook.Creator[0] != null)
                {
                    myEbook.Author = getEpubBook.Creator[0];
                }
                if (getEpubBook.Publisher[0] != null)
                {
                    myEbook.Publisher = getEpubBook.Publisher[0];
                }
                myEbook.Path = filePath;
                myEbook.ImportDate = DateTime.Now.ToString(Constants.FullDateTimeFormatToSecond);
                myEbook.Rating = 0;
                myEbook.CurrentPage = 0;
                myEbook.BookmarkPath = Constants.StorageStructure.TempFolder + Constants.CommonCharacters.SlashString +
                                       myEbook.Title + Constants.XmlExtension;
                //myEbook.PageCount = getEpubBook.epubPage;
            }
            return myEbook;
        }
    }
}
